#include "dlgrobotlist.h"
#include "ui_dlgrobotlist.h"

DlgRobotList::DlgRobotList(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DlgRobotList)
{
    ui->setupUi(this);
    this->setModal(true);
}

DlgRobotList::~DlgRobotList()
{
    delete ui;
}

QString stdToQString(const std::string &str)
{
    return QString::fromUtf8(str.c_str());
}

void DlgRobotList::updateTableWidget(const Ice::StringSeq &strlist)
{
    ui->tableWidget->clear();

    QString strMac;
    QString strSN;
    QString strVersion;
    QString strAdress;
    int State;

    int nRow = strlist.size();
    ui->tableWidget->setColumnCount(5);
    //选中行
    ui->tableWidget->setSelectionBehavior( QAbstractItemView::SelectRows);
    ui->tableWidget->setSelectionMode( QAbstractItemView::SingleSelection);
    //取消焦点
    ui->tableWidget->setFocusPolicy(Qt::NoFocus);
    QHeaderView* headerView = ui->tableWidget->verticalHeader();
    headerView->setHidden(true);//去掉自带序列号
    ui->tableWidget->setRowCount(nRow);
    QStringList header;
    header<<"MAC"<<"SN"<<"Version"<<"AVAdress"<<"State";
    ui->tableWidget->setHorizontalHeaderLabels(header);

    for(int i = 0;i<strlist.size();i++){
        QString strRobotInfo = QString::fromUtf8(strlist.at(i).c_str());
        QJsonParseError json_error;
        QJsonDocument jsDoc = QJsonDocument::fromJson(strRobotInfo.toUtf8(), &json_error);
        if(json_error.error != QJsonParseError::NoError)
        {
            return;
        }

        QJsonObject jsObj = jsDoc.object();

        strMac = jsObj.value("robotMac").toString();
        strSN = jsObj.value("sn").toString();
        strVersion = jsObj.value("version").toString();
        strAdress = jsObj.value("AVadress").toString();
        State = jsObj.value("state").toInt();
        qDebug()<<strMac<<strSN<<strVersion<<strAdress<<State;

        //添加
        QTableWidgetItem* pOneItem = NULL;

        pOneItem = new QTableWidgetItem(strMac);
        pOneItem->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget->setItem(i, 0, pOneItem);

        pOneItem = new QTableWidgetItem(strSN);
        pOneItem->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget->setItem(i, 1, pOneItem);

        pOneItem = new QTableWidgetItem(strVersion);
        pOneItem->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget->setItem(i, 2, pOneItem);

        pOneItem = new QTableWidgetItem(strVersion);
        pOneItem->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget->setItem(i, 3, pOneItem);

        if(State){
            pOneItem = new QTableWidgetItem("online");
        }else{
            pOneItem = new QTableWidgetItem("offline");
        }

        pOneItem->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget->setItem(i, 4, pOneItem);
    }
}

void DlgRobotList::on_pushButton_conn_clicked()
{
    if(!ui->tableWidget->currentItem()){
        return;
    }
    QString strState = ui->tableWidget->item(ui->tableWidget->currentRow(),4)->text();
    if(strState == "offline")
    {
        QMessageBox::warning(this, "警告", "该机器人不在线！", QMessageBox::Yes);
        return;
    }
    QString mac = ui->tableWidget->item(ui->tableWidget->currentRow(),0)->text();
    emit sig_conn(mac);
}

void DlgRobotList::on_pushButton_cancel_clicked()
{
    this->hide();
}

void DlgRobotList::on_pushButton_update_clicked()
{
    emit sig_update();
}
